/*
 * @Author: jiangxin
 * @Date: 2022-11-25 14:32:23
 * @Company: orientsec.com.cn
 * @Description:
 */
const webpack = require("webpack");
const { getDevConfig } = require("./utils/index");
const HtmlWebpackPlugin = require("html-webpack-plugin");

const currentScript = process.env.npm_lifecycle_event;

// filename:业务名称
// env:开发[dev]/生产[prod]
// action:动作 serve/build
const {
  groups: { filename, env, action = "serve" }
} = currentScript?.match?.(
  /(?<filename>\w+-{1}\w+)-{1}(?<env>\w+)(:{1}(?<action>\w+))?/i
);

const getPlugins = () => {
    console.log('action',action)
  if (action === "build") {
    return [
      new webpack.DefinePlugin({
        INJECTWINDOW: `function(){
                        window.localStorage.setItem('SERVICEENV','prod')
                        window.DEVCONFIG=${JSON.stringify(
                          getDevConfig(env, action)
                        )}
                    }`
      })
    ];
  }
};

module.exports = () => {
  return {
    mode: "production",
    devtool:"hidden-source-map",
    plugins: [
      new HtmlWebpackPlugin({
        template: "index.html",
        inject: "body"
      }),
      ...getPlugins()
    ]
  };
};

